<?php
header("Content-Type: application/rss+xml; charset=ISO-8859-1");
date_default_timezone_set('America/Los_Angeles');

$file = "../updates.txt"; 
$lastcount = 15;  // how many lines of updates to show

// FIXME: copied from ../index.php
function read_file($file, $lines)
{
    $handle = fopen($file, "r");
    fseek($handle, 0);
    $linecounter = $lines;
    $pos = -2;
    $beginning = false;
    $text = array();
    while ($linecounter > 0) {
        $t = " ";
        while ($t != "\n") {
            if(fseek($handle, $pos, SEEK_END) == -1) {
                $beginning = true; break;
            }
            $t = fgetc($handle);
            $pos--;
        }
        $linecounter--;
        if($beginning) rewind($handle);
        $text[$lines-$linecounter-1] = fgets($handle);
        if($beginning) break;
    }
    fclose ($handle);
    // array_reverse is optional: 
    // can just return the $text array which consists of the file's lines.
    //return array_reverse($text); 
    return $text; 
}


$lines = read_file($file, $lastcount);
#list( $count, $update_type, $old_id, $old_time, $old_diff_secs_max, $old_msg)= 
#    split(":",rtrim($lines[0]));

print '<?xml version="1.0" encoding="ISO-8859-1"?>'; print "\n";
print '<rss version="2.0">'; print "\n";
print "<channel>\n";
print " <title>CrashSpace Sign</title>\n";
print " <link>http://crashspacela.com/sign/</link>\n";
print " <image><width>88</width><height>88</height>\n";
print "  <url>http://crashspacela.com/sign/crashlogo-icon.gif</url>\n";
print "  <title>CrashSpace Sign</title>\n";
print "  <link>http://crashspacela.com/sign/</link>\n";
print "  <description>CrashSpace</description>\n";
print " </image>\n";
print "<description>RSS feed for CrashSpace Sign</description>\n";
print "<language>en-us</language>\n";
#print "<copyright>Copyright (C) 20010-13 crashspace.org</copyright>\n";

foreach ($lines as $line) {
    list($count,$type,$id,$time,$diff_secs_max,$msg) = explode(":",rtrim($line));
    #$tt = date( $dfmt, $time);
    #$mins = $diff_secs_max / 60;

    print '<item>';
    print '<title>' . $msg . '</title>';
    print '<description>' . $id .' - '. $type . ' - ' . ($diff_secs_max/60) . 'mins. </description>';
    print "<link>http://crashspacela.com/sign/</link>\n";
    #print '<pubDate>' . date("D, d M Y H:i:s O", strtotime($time)) . '</pubDate>';
    print '<pubDate>' . date("D, d M Y H:i:s O", $time) . '</pubDate>';
    print '</item>';
    print "\n";
    #print "<tr><td> $id </td><td>$type</td><td> $tt </td>".
    #    "<td> $mins </td><td> $msg </td></tr>\n";
}
print '</channel>';
print "</rss>\n";


